<?php
namespace app\api\controller;

use app\common\controller\BaseController;
use think\facade\Request;
use think\facade\Cache;
use think\facade\Db;
use app\common\utils\JsonResponse;
use app\common\service\UserService;


class User extends BaseController
{
    
    public $secret_key = 'G$NjE$RhrPzfkVumY3dHhjnrfyfbR998yYsx#fPdeHNjgpCv';
    /**
     * 设备ID登录
     * @return
     */
    public function deviceIdLogin() {
        $params = Request::all();
        $UserService = (new UserService());
        if (empty($params['device_id'])) return JsonResponse::error('设备ID不能为空');
        //jsonBack($UserService->_success(['is_login'=>0],'设备ID不能为空'));
        if (empty($params['secret_key'])) return JsonResponse::error('秘钥为空');
        if ($params['secret_key'] != $this->secret_key) {
            return JsonResponse::error('秘钥不正确');
        }
        $res = $UserService ->deviceIdLogin($params);
        return $res;

    }


    /**
     * 用户登录
     
    public function apilogin()
    {
        try {
            $params = Request::only(['username', 'password']);
            
            // 参数验证
            if (empty($params['username']) || empty($params['password'])) {
                return JsonResponse::error('用户名和密码不能为空');
            }
            
            // 查询用户
            $user = Db::name('t_user')
                ->where('username', $params['username'])
                ->whereNull('delete_time')
                ->find();
                
            if (!$user) {
                return JsonResponse::error('用户不存在');
            }
            
            // 验证密码
            if (!password_verify($params['password'], $user['password'])) {
                return JsonResponse::error('密码错误');
            }
            
            // 检查用户状态
            if ($user['status'] != 1) {
                return JsonResponse::error('账号已被禁用');
            }
            
            // 生成token
            $token = md5(uniqid(mt_rand(), true));
            $expireTime = time() + 86400; // 24小时后过期
            
            // 保存token
            Cache::set('user_token_' . $token, $user['id'], 86400);
            
            // 更新用户登录信息
            Db::name('t_user')->where('id', $user['id'])->update([
                'last_login_time' => time(),
                'last_login_ip' => Request::ip(),
                'update_time' => time()
            ]);
            
            // 返回用户信息
            return JsonResponse::success([
                'token' => $token,
                'user_info' => [
                    'id' => $user['id'],
                    'username' => $user['username'],
                    'nickname' => $user['nickname'],
                    'avatar' => $user['avatar']
                ]
            ], '登录成功');
            
        } catch (\Exception $e) {
            return JsonResponse::error('登录失败：' . $e->getMessage());
        }
    }
    */
    /**
     * 用户登出
     */
    public function logout()
    {
        try {
            $token = Request::header('Authorization');
            if ($token) {
                Db::name('user_token')->where('token', $token)->update(['status'=>2]);
                Cache::delete('user_token_' . $token);
            }
            return JsonResponse::success([], '退出成功');
        } catch (\Exception $e) {
            return JsonResponse::error('退出失败：' . $e->getMessage());
        }
    }
    
    /**
     * 获取用户信息
     */
    public function info()
    {
        try {
            $userId = Request::get('userId');
            
            $user = Db::name('user')
                ->field('id,username,nickname,avatar,status,last_login_time,last_login_ip')
                ->where('id', $userId)
                ->whereNull('delete_time')
                ->find();
                
            if (!$user) {
                return JsonResponse::error('用户不存在');
            }
            
            return JsonResponse::success($user);
        } catch (\Exception $e) {
            return JsonResponse::error('获取用户信息失败：' . $e->getMessage());
        }
    }

    public function messageBoard()
    {
print_r('3333');die;
    }
} 